package ms07.ticketPro.activities;

import ms07.ticketPro.R;
import ms07.ticketPro.Ticket;
import ms07.ticketPro.TicketRequest;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityTicketRequestReceived  extends Activity {

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.received_request); 
        
        Bundle extras = getIntent().getExtras();
		if(extras!=null){
			TicketRequest ticket = (TicketRequest)extras.getSerializable("ticket");
			
			this.showTicket(ticket);
		}

		Button cancel = (Button)this.findViewById(R.id.received_request_button_cancel);
		
		cancel.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	                // Perform action on click
	            	ActivityTicketRequestReceived.this.finish();
	            }
	        });
		
		Button order = (Button)this.findViewById(R.id.received_request_button_order);
			
		order.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	                // Perform action on click
	            	
	            	RadioButton member = (RadioButton)ActivityTicketRequestReceived.this.findViewById(R.id.received_request_member);
	            	RadioButton normal = (RadioButton)ActivityTicketRequestReceived.this.findViewById(R.id.received_request_normal);
	            	
	            	if(member.isChecked()){
	            		ActivityTicketRequestReceived.this.requestMemberTicket();
	            	}else if(normal.isChecked()){
	            		ActivityTicketRequestReceived.this.requestNormalTicket();
	            	}else{
	            		//show toast to select one of those!!!
	            		Toast toast = Toast.makeText(ActivityTicketRequestReceived.this, "Zum Bestellen muss ein Ticket ausgewählt werden", Toast.LENGTH_SHORT);
	            		toast.show();
	            		
	            		return;
	            	}
	            	
	            	ActivityTicketRequestReceived.this.finish();
	            }
	        });
    }

    protected void requestMemberTicket(){
    	//sende SMS mit "V"

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    	
    	SmsManager sender = SmsManager.getDefault(); 
		sender.sendTextMessage(prefs.getString("smsprovider", "082820200"),null, "V",null, null);
		
		Toast toast = Toast.makeText(ActivityTicketRequestReceived.this, "Vorteilsticket angefordert", Toast.LENGTH_SHORT);
		toast.show();
    }
    
    protected void requestNormalTicket(){
    	//sende SMS mit "N"

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    	
    	SmsManager sender = SmsManager.getDefault(); 
		sender.sendTextMessage(prefs.getString("smsprovider", "082820200"),null, "N",null, null);
		
		Toast toast = Toast.makeText(ActivityTicketRequestReceived.this, "Normales Ticket angefordert", Toast.LENGTH_SHORT);
		toast.show();
    }
    
    protected void showTicket(TicketRequest ticket){
    	TextView city_from = (TextView)this.findViewById(R.id.received_request_from);
    	city_from.setText(ticket.getCity_from());
    	
    	TextView city_to = (TextView)this.findViewById(R.id.received_request_to);
    	city_to.setText(ticket.getCity_to());
    	
    	TextView cildren = (TextView)this.findViewById(R.id.received_request_children);
    	if(ticket.getChildren()>0){
    		cildren.setText(ticket.getChildren()+"Kinder");
    	}else{
    		cildren.setText("");
    	}
    	
    	TextView adults = (TextView)this.findViewById(R.id.received_request_adults);
    	if(ticket.getAdults()>0){
    		adults.setText(ticket.getAdults()+"Erw");
    	}else{
    		adults.setText("");
    	}
    	
    	RadioButton member = (RadioButton)this.findViewById(R.id.received_request_member);
    	member.setText("Vorteilscard ("+ticket.getPrice_member()+"EUR)");
    	
    	RadioButton normal = (RadioButton)this.findViewById(R.id.received_request_normal);
    	normal.setText("Normal ("+ticket.getPrice_normal()+"EUR)");
    }
}
